/*
 * QTextCharFormatAdapter.cpp  0.4.0 / Nov 30, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
#include "QTextCharFormatAdapter.h"

// QTextCharFormatAdapter :: Public Constructor. -------------------------------

/*!
 * \brief Constructs new QTextCharFormatAdapter object.
 *
 * \param format the CharacterFormat object
 */
QTextCharFormatAdapter::QTextCharFormatAdapter(jobject format)
  : QTextCharFormat()
{
  if (format) {
    jobject font;
    jboolean fontBold;
    jobject color;
    if (JNISwingWrapper::callObjectMethod(font, format, sm_getFontMID) &&
        JNISwingWrapper::callBooleanMethod(fontBold, format,
                                           sm_isFontBoldMID) &&
        JNISwingWrapper::callObjectMethod(color, format, sm_getColorMID)) {
      if (font) {
        setFont(*QFontAdapter::toQFont(font));
      }
      if (fontBold) {
        setFontWeight(QFont::Bold);
      }
      if (color) {
        setForeground(QColorAdapter(color));
      }
    }
  }
}

// QTextCharFormatAdapter :: Public Static Member Function. --------------------

/*!
 * \brief Initializes QTextCharFormatAdapter class.
 *
 * \param env the JNIEnv pointer
 * \param cls the CharacterFormat class
 *
 * \exception FatalError if failed
 */
void QTextCharFormatAdapter::initialize(JNIEnv *env, jclass cls)
{
  Q_ASSERT(env);
  Q_ASSERT(cls);
  Q_ASSERT(!sm_getFontMID);
  if (!INIT_MID(getFont, cls, "()Lcom/cute4j/swing/graphics/Font;") ||
      !INIT_MID(isFontBold, cls, "()Z") ||
      !INIT_MID(getColor, cls, "()Lcom/cute4j/swing/graphics/Color;")) {
    FATAL_LOGGED_ERROR("QTextCharFormatAdapter initialization failed");
  }
}

// QTextCharFormatAdapter :: Private Static Data Members. ----------------------

jmethodID QTextCharFormatAdapter::sm_getFontMID = 0;
jmethodID QTextCharFormatAdapter::sm_isFontBoldMID = 0;
jmethodID QTextCharFormatAdapter::sm_getColorMID = 0;
